package henu.interceptor;


import henu.bean.ManagePerson;
import henu.util.CONSTANTS;

import java.io.IOException;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Description: 拦截admin文件夹下所有资源
 * @author <oaksharks@163.com>
 */
@WebFilter(dispatcherTypes = {DispatcherType.REQUEST }
					, urlPatterns = { "/admin/*" })
public class AdminFilter implements Filter {

    /**
     * Default constructor. 
     */
    public AdminFilter() 
    {
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() 
	{
	}

	/**
	 * Description :用于检验用户是否为空 ，如果为空跳转到
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req=(HttpServletRequest)request;
		HttpSession session = req.getSession(); 
		ManagePerson admin =(ManagePerson)req.getSession().getAttribute(CONSTANTS.SESSION_ADMIN);
		/*登录界面以及登录界面所需要的相关资源是不能拦截的*/
		if(admin==null)
		{
			((HttpServletResponse)response).sendRedirect("/aqjy/public/Adminlogin.jsp");
		}
		chain.doFilter(request, response); /*只能调用一次*/ 
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
	}

}
